package com.mercury.ec.module.air.controller.admin.demandorder;

import com.mercury.ec.framework.apilog.core.annotation.ApiAccessLog;
import com.mercury.ec.framework.common.pojo.CommonResult;
import com.mercury.ec.framework.common.pojo.PageParam;
import com.mercury.ec.framework.common.pojo.PageResult;
import com.mercury.ec.framework.excel.core.util.ExcelUtils;
import com.mercury.ec.module.air.controller.admin.demandorder.vo.demandorder.*;
import com.mercury.ec.module.air.service.demandorder.AirDemandOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static com.mercury.ec.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.mercury.ec.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 机票需求订单")
@RestController
@RequestMapping("/air/demand-order")
@Validated
public class AirDemandOrderController {

    @Resource
    private AirDemandOrderService airDemandOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建机票需求订单")
    @PreAuthorize("@ss.hasPermission('air:demand-order:create')")
    public CommonResult<Long> createAirDemandOrder(@Valid @RequestBody AirDemandOrderCreateReqVO createReqVO) {
        return success(airDemandOrderService.createAirDemandOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新机票需求订单")
    @PreAuthorize("@ss.hasPermission('air:demand-order:update')")
    public CommonResult<Boolean> updateAirDemandOrder(@Valid @RequestBody AirDemandOrderUpdateReqVO updateReqVO) {
        airDemandOrderService.updateAirDemandOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除机票需求订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('air:demand-order:delete')")
    public CommonResult<Boolean> deleteAirDemandOrder(@RequestParam("id") Long id) {
        airDemandOrderService.deleteAirDemandOrder(id);
        return success(true);
    }

    @PutMapping("/submit")
    @Operation(summary = "提交机票需求订单")
    @PreAuthorize("@ss.hasPermission('air:demand-order:submit')")
    public CommonResult<Boolean> submitAirDemandOrder(@Valid @RequestBody AirDemandOrderSubmitReqVO submitReqVO) {
        airDemandOrderService.submitAirDemandOrder(submitReqVO);
        return success(true);
    }

    @PutMapping("/cancel")
    @Operation(summary = "取消机票需求订单")
    @PreAuthorize("@ss.hasPermission('air:demand-order:cancel')")
    public CommonResult<Boolean> cancelAirDemandOrder(@Valid @RequestBody AirDemandOrderCancelReqVO cancelReqVO) {
        airDemandOrderService.cancelAirDemandOrder(cancelReqVO);
        return success(true);
    }

    @PutMapping("/lock")
    @Operation(summary = "锁定机票需求订单")
    @PreAuthorize("@ss.hasPermission('air:demand-order:lock')")
    public CommonResult<Boolean> lockAirDemandOrder(@Valid @RequestBody AirDemandOrderLockReqVO lockReqVO) {
        airDemandOrderService.lockAirDemandOrder(lockReqVO);
        return success(true);
    }

    @PutMapping("/unlock")
    @Operation(summary = "解锁机票需求订单")
    @PreAuthorize("@ss.hasPermission('air:demand-order:unlock')")
    public CommonResult<Boolean> unlockAirDemandOrder(@Valid @RequestBody AirDemandOrderLockReqVO lockReqVO) {
        airDemandOrderService.unlockAirDemandOrder(lockReqVO);
        return success(true);
    }

    @PutMapping("/quote")
    @Operation(summary = "机票需求订单报价")
    @PreAuthorize("@ss.hasPermission('air:demand-order:quote')")
    public CommonResult<Boolean> quoteAirDemandOrder(@Valid @RequestBody AirDemandOrderQuoteReqVO quoteReqVO) {
        airDemandOrderService.quoteAirDemandOrder(quoteReqVO);
        return success(true);
    }

    @PutMapping("/pricing")
    @Operation(summary = "机票需求订单核价")
    @PreAuthorize("@ss.hasPermission('air:demand-order:pricing')")
    public CommonResult<Boolean> pricingAirDemandOrder(@Valid @RequestBody AirDemandOrderPricingReqVO pricingReqVO) {
        airDemandOrderService.pricingAirDemandOrder(pricingReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得机票需求订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('air:demand-order:query')")
    public CommonResult<AirDemandOrderRespVO> getAirDemandOrder(@RequestParam("id") Long id) {
        return success(airDemandOrderService.getAirDemandOrder(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得机票需求订单分页")
    @PreAuthorize("@ss.hasPermission('air:demand-order:query')")
    public CommonResult<PageResult<AirDemandOrderRespVO>> getAirDemandOrderPage(@Valid AirDemandOrderPageReqVO pageReqVO) {
        return success(airDemandOrderService.getAirDemandOrderPage(pageReqVO));
    }

    @GetMapping("/count-by-status")
    @Operation(summary = "获得机票需求订单按状态分类汇总")
    @PreAuthorize("@ss.hasPermission('air:demand-order:query')")
    public CommonResult<Map<Integer, Long>> getAirDemandOrderCountByStatus(@Valid AirDemandOrderPageReqVO pageReqVO) {
        return success(airDemandOrderService.getAirDemandOrderCountByStatus(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出机票需求订单 Excel")
    @PreAuthorize("@ss.hasPermission('air:demand-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAirDemandOrderExcel(@Valid AirDemandOrderPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AirDemandOrderRespVO> list = airDemandOrderService.getAirDemandOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "机票需求订单.xls", "数据", AirDemandOrderRespVO.class, list);
    }

}
